package com.fund.datacopy.utils;

import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;

public class DBUtils {
	public static Connection getConnection(String driverName, String url, String username, String password)
			throws Exception {
		Class.forName(driverName);
		return DriverManager.getConnection(url, username, password);
	}

	public static Connection getJuchaoConn() throws Exception {
		String driverName = "oracle.jdbc.OracleDriver";
		String url = "jdbc:oracle:thin:@122.144.134.2:1521:csdb001";
		String username = "JUCHAO";
		String password = "chinascope1234";
		return getConnection(driverName, url, username, password);
	}

	public static Connection getMysqlConn() throws Exception {
		String driverName = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://192.168.250.208:3306/fund?useUnicode=true&amp;characterEncoding=UTF-8";
		String password = "ada_user";
		String username = "ada_user";
		return getConnection(driverName, url, username, password);
	}

	public static Connection getTestConn() throws Exception {
		String driverName = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://192.168.250.208:3306/test?useUnicode=true&amp;characterEncoding=UTF-8";
		String password = "ada_user";
		String username = "ada_user";
		return getConnection(driverName, url, username, password);
	}

	public static DBCollection getMongoConn(String dbname, String coll) throws UnknownHostException {
		return getMongoConn("192.168.250.200", dbname, coll);
	}

	public static DBCollection getMgtMongoConn(String dbname, String coll) throws UnknownHostException {
		return getMongoConn("122.144.134.95", dbname, coll);
	}

	public static DBCollection getNewsMongoConn(String dbname, String coll) throws UnknownHostException {
		return getMongoConn("192.168.250.208", dbname, coll);
	}

	public static DBCollection getLocalMongoConn(String dbname, String coll) throws UnknownHostException {
		return getMongoConn("192.168.0.105", dbname, coll);
	}

	public static Set<String> getMongoCollections(String dbname) throws UnknownHostException {
		return getMongoCollections("192.168.250.200", dbname);
	}

	public static Set<String> getMongoCollections(String host, String dbname) throws UnknownHostException {
		List<ServerAddress> seeds = new ArrayList();
		seeds.add(new ServerAddress(host, 27017));
		MongoClient mongoClient = new MongoClient(seeds);
		DB db = mongoClient.getDB(dbname);
		return db.getCollectionNames();
	}

	public static DBCollection getMongoConn(String host, String dbname, String coll) throws UnknownHostException {
		List<ServerAddress> seeds = new ArrayList();
		seeds.add(new ServerAddress(host, 27017));
		MongoClient mongoClient = new MongoClient(seeds);
		DB db = mongoClient.getDB(dbname);
		return db.getCollection(coll);
	}

	public static DBCollection getMongoConn2(String host, String dbname, String coll) throws UnknownHostException {
		MongoClientOptions.Builder builder = MongoClientOptions.builder();
		MongoClientOptions mongoClientOptions = builder.build();
		List<ServerAddress> seeds = new ArrayList();
		seeds.add(new ServerAddress(host, 27017));
		MongoClient mongoClient = new MongoClient(seeds, mongoClientOptions);
		DB db = mongoClient.getDB(dbname);
		return db.getCollection(coll);
	}
}
